import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.aliasi.chunk.Chunk;
import com.aliasi.chunk.Chunking;
import com.aliasi.chunk.RegExChunker;
import com.aliasi.dict.DictionaryEntry;
import com.aliasi.dict.ExactDictionaryChunker;
import com.aliasi.dict.MapDictionary;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;


public class SolutionLingpipeDictionary {
	private static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
	
	private static void readDictionary(String filePath,MapDictionary<String> dictionary)
    throws java.io.IOException{
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        String line;
        while ((line = reader.readLine()) != null)   {
            String[] lineSplit = line.split("<\\\\|<|>");
            dictionary.addEntry(new DictionaryEntry<String>(lineSplit[2],lineSplit[1],0));
        }
        reader.close();
    }
	
	
	public static void main(String[] args) throws IOException{
		String testingFile = readFileAsString("C:\\Users\\Goncalo\\Desktop\\Teste.txt");
		
		MapDictionary<String> dictionary = new MapDictionary<String>();
		readDictionary("C:\\Users\\Goncalo\\Desktop\\dict.txt",dictionary);
		ExactDictionaryChunker dictionaryChunkerFT = new ExactDictionaryChunker(dictionary, IndoEuropeanTokenizerFactory.FACTORY,false,true); 
		
		Chunking chunking = dictionaryChunkerFT.chunk(testingFile);
		for (Chunk chunk : chunking.chunkSet()) {
			System.out.println(testingFile.substring(chunk.start(),chunk.end()));
        }
	}
}
